import { HTML5, WECHAT, BYTEDANCE } from 'cc/env';
import { wxUserLogin, htmlUserLogin, byteDanceUserLogin, ByteDanceUserLogin } from '../services/UserLogin';
import { PlatformSetting } from './PlatformSetting';
// 不同平台登陆方式不一样，这里用一个类封装起来
export class UserLogin {
  public uniLogin() {
    if (HTML5) { 
      this.htmlLogin();
    } else if (WECHAT) {
      this.wxLogin();
    } else if (BYTEDANCE){
      this.byteDanceLogin();
    }
  }
  public wxLogin() {
    wx.login({
      success: function(res: any) {
        console.log(res.code);
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wxUserLogin(res.code);
        }
      },
      fail: function(errInfo: any) {
        console.log('wx login failed will be retry ', errInfo);
      },
    });
  }
  public htmlLogin() {
    htmlUserLogin();
  }
  public byteDanceLogin() {
    tt.login({
      force: true,
      success(res) {
        console.log(`bd login 调用成功${res.code} ${res.anonymousCode}`);
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          ByteDanceUserLogin(res.code);
        }
      },
      fail(res) {
        console.log(`bd login 调用失败`);
      },
    });
  }
}